package com.shuai.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuai.model.form.AllotForm;
import com.shuai.model.loginInfo.UserDetailInfo;
import com.shuai.model.po.SysUser;
import com.shuai.model.query.SysUserQuery;
import com.shuai.model.vo.SysUserVo;
import com.shuai.service.SysUserService;
import com.shuai.utils.redis.RedisUtil;
import com.shuai.utils.returnVo.ReturnModel;
import com.shuai.utils.user.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户信息 前端控制器
 *
 * @author shuai
 * @since 2022
 */
@Slf4j
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public ReturnModel<Object> getUserInfo(){
        UserDetailInfo userDetailInfo = sysUserService.getUserInfo();
        return ReturnModel.successInstance(userDetailInfo);
    }


    /**
     * 用户退出
     * @return
     */
    @PostMapping("/logout")
    public ReturnModel<Object> logout(HttpServletRequest request, HttpServletResponse response){
        //获取token
        String token = request.getParameter("token");
        //如果没有从头部获取token，那么从参数里面获取
        if(StrUtil.isEmpty(token)){
            token = request.getHeader("token");
        }
        //获取用户相关信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(UserUtil.getSecurityContext() != null){
            //清空用户信息
            new SecurityContextLogoutHandler().logout(request,response,authentication);
            //清空redis里面的token
            String key = "token_"+token;
            redisUtil.del(key);
        }
        return ReturnModel.successInstance("退出登录成功!");
    }

    /**
     * 添加用户信息
     * @param sysUser
     * @return
     */
//    @PreAuthorize("hasAuthority('sys:user:add')")
    @PostMapping ("/addSysUser")
    public ReturnModel<Object> addSysUser(@RequestBody SysUser sysUser){
        sysUserService.addSysUser(sysUser);
        return ReturnModel.successInstance();
    }

    /**
     * 删除用户信息
     * @param query
     * @return
     */
    @RequestMapping ("/deleteSysUser")
    public ReturnModel<Object> deleteSysUser(@RequestBody SysUserQuery query){
        sysUserService.deleteSysUser(query);
        return ReturnModel.successInstance();
    }


    /**
     * 更新用户信息
     * @param sysUser
     * @return
     */
    @PostMapping("/editSysUser")
    public ReturnModel<Object> editSysUser(@RequestBody SysUser sysUser){
        sysUserService.editSysUser(sysUser);
        return ReturnModel.successInstance();
    }

    /**
     * 查询用户信息
     * @param query
     * @return
     */
    @PostMapping("/selectList")
    public ReturnModel<List<SysUserVo>> selectList(SysUserQuery query){
        List<SysUserVo> list = sysUserService.selectList(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 分页查询用户信息
     * @param query
     * @return
     */
    @PostMapping ("/selectPage")
    public ReturnModel<IPage<SysUserVo>> selectPage(@RequestBody SysUserQuery query){
        IPage<SysUserVo> list = sysUserService.selectPage(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 查询详细
     * @param id
     * @return
     */
    @GetMapping ("/selectById")
    public ReturnModel<SysUserVo> selectById(@RequestParam() Long id){
        SysUserVo userVo = sysUserService.selectById(id);
        return ReturnModel.successInstance(userVo);
    }

    /**
     * 判断用户账号是否已存在
     * @param username
     * @return
     */
    @GetMapping ("/checkUsername")
    public ReturnModel<Object> checkUsername(@RequestParam() String username){
        boolean b = sysUserService.checkUsername(username);
        return ReturnModel.successInstance(b);
    }

    /**
     * 分配角色
     * @param form
     * @return
     */
    @PostMapping("/allotRole")
    public ReturnModel<Object> allotRole(@RequestBody AllotForm form){
        sysUserService.allotRole(form);
        return ReturnModel.successInstance();
    }
}

